/*
 * This software is released under the GNU Lesser General Public License v3.
 * For more information see http://www.gnu.org/licenses/lgpl.html
 *
 * Copyright (c) 2011, Peter Knego & Matjaz Tercelj
 * All rights reserved.
 */

package com.leanengine;

import java.util.Map;

public class LeanAccount {

    private Long id;
    private String nickName;
    private String providerId;
    private String provider;
    private Map<String, Object> providerProperties;

    protected LeanAccount(Long id, String nickName, String providerId, String provider, Map<String, Object> providerProperties) {
        this.id = id;
        this.nickName = nickName;
        this.providerId = providerId;
        this.provider = provider;
        this.providerProperties = providerProperties;
    }

    /**
     * Logs user out of server. Current authentication tokens are removed on server and on client.
     *
     * @return Returns true if user was logged in and was logged out successfully.
     * @throws LeanException
     */
    public static Boolean logout() throws LeanException {
        return RestService.getInstance().logout();
    }

    /**
     * Logs user out of server. Current authentication tokens are removed on server and on client.
     * Runs on the background thread.
     *
     * @param callback Callback to be invoked when logout finishes.
     * @throws LeanException
     */
    public static void logoutInBackground(NetworkCallback<Boolean> callback) {
        RestService.getInstance().logoutAsync(callback);
    }

    /**
     * Checks if user is logged in.
     *
     * @return Returns true if user is logged in, false otherwise.
     */
    public static boolean isUserLoggedIn() {
        return LeanEngine.getAuthToken() != null;
    }

    /**
     * Gets current user account.
     *
     * @return LeanAccount of the currently logged-in user.
     * @throws LeanException
     */
    public static LeanAccount getAccountData() throws LeanException {
        return RestService.getInstance().getCurrentAccountData();
    }


    /**
     * Returns the internal Id of the account. Account Id is server generated and is unique per account.
     *
     * @return Id of the account.
     */
    public Long getId() {
        return id;
    }

    /**
     * User ID as provided by authentication provider. Ids are guaranteed to be unique within provider,
     * but not across different providers.
     * @return User ID generated by authentication provider.
     */
    public String getProviderId() {
        return providerId;
    }

    /**
     * Returns the identification string of authentication provider that authenticated this account.
     * <br/><br/>
     * Identification string may contain only provider unique string, e.g. {@code 'fb-oauth'} for Facebook,
     * or it might contain full URL including user identification, e.g.
     * {@code 'https://www.google.com/accounts/o8/id?id=user_id_hash'} in case of Google OpenID.
     *
     * @return Provider authentication string.
     */
    public String getProvider() {
        return provider;
    }

    /**
     * Nickname of the user represented by this account. Format is provider dependent.
     * Some providers use email as nick name.
     * @return Nickname.
     */
    public String getNickName() {
        return nickName;
    }

    /**
     * Returns provider-dependent map of user properties.
     * This may contain data such as gender, first name, last name, etc..
     * @return Map of properties.
     */
    public Map<String, Object> getProviderProperties() {
        return providerProperties;
    }
}
